package cn.com.callback

import groovy.json.*;
import groovy.swing.impl.DefaultAction
import org.springframework.jdbc.core.JdbcTemplate

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import cn.com.callback.CallBackInvoke
import cn.com.callback.ServiceBackInvoke
import cn.com.elite.EliteBean;
import cn.com.elite.Util;
import java.security.MessageDigest;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.kh.getProperty;
import cn.com.kh.KHUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.json.JSONException;

/**
 *
 * @Title: ApiBPOverview* @Description:深圳宝安排水小程序查询案件总览--外部接口
 * @author: chieh
 * @date 2020-11-11 21:28:00
 *
 */
public class ApiBPOverview implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(ApiBPOverview.class);

    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response) {

        log.debug("-------------------------start apiBPOverview 开始-----------------");

        long startTime = System.currentTimeMillis();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=UTF-8");
        JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());
        String reJson = null;
        long endTime = 0;

        //获取参数phone
        def openid = request.getParameter("openid");
        log.debug("解析信息-openid：" + openid);

        try {
            if (openid == null || openid == "" || openid == "undefined") {
                endTime = System.currentTimeMillis();
                reJson = "{\"resultcode\":\"0\",\"resultmessage\":\"处理失败(phone必填字段;),调用问题处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":[]}";
                return reJson;
            }

            String selectSQL = '''Select Count(*) As Reportedcount,
                                   Ifnull(Sum(If(Deal_Status = 0, 1, 0)), 0) As Handlecount,
                                   Ifnull(Sum(If(Deal_Status = 1, 1, 0)), 0) As Finishcount,
                                   Ifnull(Sum(If(Deal_Status = -1, 1, 0)), 0) As Nofinishcount
                              From Wx_Uploads_List
                             Where Open_Id = ?''';
            List<Map<String, Object>> listcountresult = jt.queryForList(selectSQL, openid);

            def reportedcount = 0;
            def handlecount = 0;
            def finishcount = 0;
            def nofinishcount = 0;
            def count = listcountresult.size();
            if (count > 0) {
                reportedcount = listcountresult.get(0).get("Reportedcount");
                handlecount = listcountresult.get(0).get("Handlecount");
                finishcount = listcountresult.get(0).get("Finishcount");
                nofinishcount = listcountresult.get(0).get("Nofinishcount");
            }
            JSONArray jsonArray = new JSONArray();
            // json对象
            JSONObject json = new JSONObject();
            if (reportedcount == null) {
                reportedcount = 0;
            }
            if (handlecount == null) {
                handlecount = 0;
            }
            if (finishcount == null) {
                finishcount = 0;
            }
            if (nofinishcount == null) {
                nofinishcount = 0;
            }

            json.put("applied", reportedcount);
            json.put("handle", handlecount);
            json.put("complete", finishcount);
            json.put("nocomplete", nofinishcount);

            jsonArray.add(json);

            endTime = System.currentTimeMillis();
            reJson = "{\"resultcode\":\"1\",\"resultmessage\":\"获取工单总览接口调用处理时长(毫秒):" + (endTime - startTime) + "\",\"data\":" + jsonArray.toString() + "}";

        } catch (Exception e) {
            log.error("接口处理异常" + e.getMessage());
            // 失败返回
            reJson = "{\"resultcode\":\"0\",\"resultmessage\":\"获取工单总览接口调用处理失败,请检查确认入参有效且合法\",\"data\":[]}";

        }
        return reJson;

    }
}